﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ExameRegister.Views;
using ExameRegister.Model;

namespace ExameRegister
{
    public partial class PositionList : UserControl
    {
        //private WCFService.WCFServiceClient client = null;
        ServiceUtility su = new ServiceUtility();
        private int _ExameID = 0;
        public PositionList(int ExameID)
        {
            InitializeComponent();
            _ExameID = ExameID;
            su.OnCompletedCallService += new EventHandler<WCFEvent>(su_OnCompletedCallService);
            if (Sessions.Session["username"].Equals(Sessions.ADMIN))
            {
                su.CallService("ExameRegister.BO.LoginBO", "GetRegisterPosition", new object[] { Sessions.Session["idcard"] as string, Sessions.Session["username"] as string, 4, (int)Sessions.Session[Sessions.EXAMENATIONTYPE], _ExameID }, this, "GetRegisterPosition");
                return;
            }
            //判断是否填写简历
            su.CallService("ExameRegister.BO.LoginBO", "HasWritCV", new object[] { 0, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string }, this, "HasWritCV");
            //su.CallService("ExameRegister.BO.LoginBO", "GetRegisterPosition", new object[] { Sessions.Session["idcard"] as string, Sessions.Session["username"] as string, 4, (int)Sessions.Session[Sessions.EXAMENATIONTYPE] }, this, "GetRegisterPosition");
            //client = new WCFService.WCFServiceClient();
            //client.GetPublicOfficialSelectionCompleted+=new EventHandler<WCFService.GetPublicOfficialSelectionCompletedEventArgs>(client_GetPublicOfficialSelectionCompleted);
            //client.GetPublicOfficialSelectionAsync("Status=4");
        }

        void su_OnCompletedCallService(object sender, WCFEvent e)
        {
            string code = e.UserState as string;
            switch (code)
            {
                case "HasWritCV":
                    bool hasCV = Utility.ConvertFromJson<bool>(e.Result);
                    if (!hasCV)
                        Button_Click(null, null);
                        //su.CallService("ExameRegister.BO.LoginBO", "GetRegisterPosition", new object[] { Sessions.Session["idcard"] as string, Sessions.Session["username"] as string, 4, (int)Sessions.Session[Sessions.EXAMENATIONTYPE] }, this, "GetRegisterPosition");
                    else
                       su.CallService("ExameRegister.BO.LoginBO", "GetRegisterPosition", new object[] { Sessions.Session["idcard"] as string, Sessions.Session["username"] as string, 4, (int)Sessions.Session[Sessions.EXAMENATIONTYPE], _ExameID }, this, "GetRegisterPosition");
                    break;
                case "GetRegisterPosition":
                    try
                    {
                        dgPosition.ItemsSource = null;
                        ItemsSource = Utility.ConvertFromJson<System.Collections.ObjectModel.ObservableCollection<Model.RegisterPosition>>(e.Result);
                        dgPosition.ItemsSource = ItemsSource;
                    }
                    catch (Exception ex)
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage(ex.Message);
                    }
                    break;
                case "JudgeCodition":

                    String result = e.Result;
                    if (string.IsNullOrEmpty(result))
                    {
                        //PersonnelInfoWindow piw = new PersonnelInfoWindow(positionid, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, status);
                        //piw.Closed += new EventHandler(piw_Closed);
                        //piw.Show();
                        SilverlightMessageBoxLibrary.Message.InfoMessage("操作成功");
                        su.CallService("ExameRegister.BO.LoginBO", "GetRegisterPosition", new object[] { Sessions.Session["idcard"] as string, Sessions.Session["username"] as string, 4, (int)Sessions.Session[Sessions.EXAMENATIONTYPE], _ExameID }, this, "GetRegisterPosition");
                    }
                    else
                    {
                        SilverlightMessageBoxLibrary.Message.InfoMessage("未满足报名条件:" + result);
                    }
                    positionid = 0;
                    break;
            }
        }

        void piw_Closed(object sender, EventArgs e)
        {

            su.CallService("ExameRegister.BO.LoginBO", "GetRegisterPosition", new object[] { Sessions.Session["idcard"] as string, Sessions.Session["username"] as string, 4, (int)Sessions.Session[Sessions.EXAMENATIONTYPE], _ExameID }, this, "GetRegisterPosition");
        }

        private System.Collections.ObjectModel.ObservableCollection<RegisterPosition> ItemsSource = null;
        //void client_GetPublicOfficialSelectionCompleted(object sender, WCFService.GetPublicOfficialSelectionCompletedEventArgs e)
        //{
        //    dgPosition.ItemsSource = null;
        //    ItemsSource = e.Result;
        //    dgPosition.ItemsSource = e.Result;
        //}

        

        public void BeginReturn()
        {

        }

        public string Title
        {
            get
            ;
            set
            ;
        }

        public bool PageLoaded
        {
            get
            ;
            set
            ;
        }

        public Assert.NavMoveSwitch Nav
        {
            get
            ;
            set
            ;
        }

       
        private void dgPosition_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            HyperlinkButton hlbD = dgPosition.Columns[9].GetCellContent(e.Row).FindName("description") as HyperlinkButton;
            HyperlinkButton hlbR = dgPosition.Columns[10].GetCellContent(e.Row).FindName("register") as HyperlinkButton;
            int index = e.Row.GetIndex();
            var entity = ItemsSource[index];
            if (hlbD != null)
            {
                hlbD.Tag = entity;
            }
            if (hlbR != null)
            {
                switch (entity.Status)
                {
                    case 0:
                        hlbR.Content = "撤销报名";
                        break;
                    case 1:
                    case 4:
                   
                        hlbR.Content = "不能操作";
                        break;
                    case 2:
                    case 3:
                    default:
                        hlbR.Content = "报名";
                        break;
                }
                //if (entity.Status == 2 || entity.Status == 1)
                //    hlbR.Content = "查看";
                //else if (entity.Status == 0 || entity.Status == 3)
                //    hlbR.Content = "编辑";
                hlbR.Tag = entity;
            }
        }

       

        private void description_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton hlb = sender as HyperlinkButton;
            var entity = hlb.Tag as RegisterPosition;
            PublicOfficialSelectionWindow win = new PublicOfficialSelectionWindow(entity.PositionID);
            win.Show();
            //PositionWindow win = new PositionWindow(entity.PositionID);
            //win.Show();
        }

        /// <summary>
        /// 报名，点击报名之前，进行报名条件判断
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private int positionid = 0;
        private int status = 0;
        private void register_Click(object sender, RoutedEventArgs e)
        {
            if (Sessions.Session["username"].Equals(Sessions.ADMIN))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("您不能报名");
                return;
            }
            positionid = 0;
            HyperlinkButton hlb = sender as HyperlinkButton;
            var entity = hlb.Tag as RegisterPosition;
            SilverlightMessageBoxLibrary.CustomMessage cm = null;
            /// 0 审核中
            /// 1 审核通过
            /// 2 退回
            /// 3 暂存
            /// 4 已有岗位通过审核
            switch (entity.Status)
            {
                case 0:
                    //SilverlightMessageBoxLibrary.Message.ErrorMessage("已报名，不能修改!");
                    cm = new SilverlightMessageBoxLibrary.CustomMessage("确认取消报名?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                    cm.OKButton.Click += (sender1, e1) =>
                    {
                        su.CallService("ExameRegister.BO.LoginBO", "JudgeCodition", new object[] { entity.EmpID, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, entity.PositionID, 3, _ExameID }, this, "JudgeCodition");

                    };
                        break;
                case 1:
                case 4:
                    SilverlightMessageBoxLibrary.Message.ErrorMessage("不能修改!");
                    break;
                case 2:
                     cm = new SilverlightMessageBoxLibrary.CustomMessage("确认报名?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                    cm.OKButton.Click += (sender1, e1) =>
                    {
                        su.CallService("ExameRegister.BO.LoginBO", "JudgeCodition", new object[] { entity.EmpID, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, entity.PositionID, 0, _ExameID }, this, "JudgeCodition");

                    };
                    //SilverlightMessageBoxLibrary.Message.InfoMessage("不能操作");
                    //if (MessageBox.Show("确认?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    //{
                    //    su.CallService("ExameRegister.BO.LoginBO", "JudgeCodition", new object[] { entity.EmpID, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, entity.PositionID, 0, _ExameID }, this, "JudgeCodition");
                    //}
                break;
                case 3:
                default:
                     cm = new SilverlightMessageBoxLibrary.CustomMessage("确认报名?", SilverlightMessageBoxLibrary.CustomMessage.MessageType.Confirm, null);
                    cm.OKButton.Click += (sender1, e1) =>
                    {
                        su.CallService("ExameRegister.BO.LoginBO", "JudgeCodition", new object[] { entity.EmpID, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, entity.PositionID, 0, _ExameID }, this, "JudgeCodition");

                    };
                //if (MessageBox.Show("确认?", "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                //{
                //    su.CallService("ExameRegister.BO.LoginBO", "JudgeCodition", new object[] { entity.EmpID, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, entity.PositionID, 0, _ExameID }, this, "JudgeCodition");
                //}
                break;
            }
            cm.Show();
            positionid = entity.PositionID;
            status = entity.Status;
            //PersonnelInfoWindow piw = new PersonnelInfoWindow(positionid, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, entity.Status);
            //piw.Closed += new EventHandler(piw_Closed);
            //piw.Show();
            
        }

		/// <summary>
		/// 简历管理
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (Sessions.Session["username"].Equals(Sessions.ADMIN))
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("您不能查看简历信息!");
                return;
            }
        	// 在此处添加事件处理程序实现。
            var piw = new PersonnelInfoWindow(0, Sessions.Session["username"] as string, Sessions.Session["idcard"] as string, 0);
            piw.Show();
            piw.Closed+=new EventHandler(piw_Closed);
        }
    }
}
